#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import json


class ErrorCodeException(Exception):
    def __init__(self, error_code: int, *parameters, message: str = None):
        super().__init__(message)
        self._error_code = error_code
        self._parameters = parameters
        self._message = message

    @property
    def error_code(self) -> int:
        return self._error_code

    @property
    def parameters(self) -> list[str]:
        # 每个错误码参数最多展示300个字符
        return [str(p)[:300] for p in self._parameters] if self._parameters else []

    @property
    def error_message(self) -> str:
        return json.dumps({"errorCode": self._error_code, "parameters": self._parameters,
                           "errorMessage": self._message})
